<template>
	<view class="page">
		<view class="list" v-if="!showNext">
			<view class="item" style="flex-direction: column;align-items: flex-start;height: auto;">
				<view class="label">请完成以下认证</view>
				<view class="content">请输入{{mobile}}收到的短信验证码</view>
			</view>
			<view class="item">
				<input type="text" placeholder="请输入验证码" v-model="code" />
				<text class="getCode" @click="getCode">
					{{text}}
				</text>
			</view>
			<view class="logout" @click="next">
				下一步
			</view>
		</view>
		<view class="list" v-else>
			<view class="item">
				<view class="label">请关联新手机号</view>
			</view>
			<view class="item">
				<input type="text" placeholder="请输入新手机号" v-model="model.mobile" />
			</view>
			<view class="item">
				<input type="text" placeholder="请输入验证码" v-model="model.code" />
				<text class="getCode" @click="getCode">
					{{text}}
				</text>
			</view>
			<view class="logout" @click="submit">
				完成
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showNext: false,
				mobile: '',
				showMobile: '',
				code: '',
				model: {
					mobile: '',
					code: ''
				},
				text: '获取验证码',
				userInfo:{}
			}
		},
		onLoad() {
			this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
			console.log(this.userInfo,"this.userInfo")
			this.mobile = this.userInfo.mobile
			if(this.mobile){
				this.showMobile = this.mobile.slice(0, 3) + '****' + this.mobile.slice(7);
			}
		},
		methods: {
			go(url) {
				uni.navigateTo({
					url: url
				})
			},
			//验证码倒计时
			getCode() {
				if (this.showNext) {
					if (this.model.mobile == '') {
						uni.showToast({
							title: "请输入手机号~"
						})
						return
					}
					if (!(/^1(3[0-9]|4[5,7]|5[0,1,2,3,5,6,7,8,9]|6[2,5,6,7]|7[0,1,7,8,6]|8[0-9]|9[1,8,9])\d{8}$/.test(this
							.model.mobile))) {
						uni.showToast({
							title: "手机号格式错误~"
						})
						return
					}
				}
				let mobile = this.showNext ? this.model.mobile : this.mobile
				if (this.text == '获取验证码' || this.text == '重新获取') {
					this.rq.getData('sms/send', {
						mobile: mobile,
						event: 'editPhone',
					}).then((res) => {
						let n = 60;
						this.text = "剩余 " + n + "s";
						const run = setInterval(() => {
							n = n - 1;
							this.text = "剩余 " + n + "s";
							if (n < 1) {
								clearInterval(run);
								this.text = "重新获取";
							}
						}, 1000);
					});
				}
			},
			next(){
				this.rq.getData('user/oldMobile',{
					mobile:this.mobile,
					code:this.code
				}).then(res=>{
					if(res.code === 1){
						this.text = '获取验证码'
						this.showNext = true
					}
				})
			},
			submit() {
				this.rq.getData('user/changeMobile',this.model).then(res=>{
					if(res.code === 1){
						this.userInfo.mobile = this.model.mobile
						uni.setStorageSync('userInfo',JSON.stringify(this.userInfo))
						uni.showToast({
							title:'修改成功~',
							icon:'none'
						})
						setTimeout(()=>{
							uni.navigateBack()
						},500)
					}
				})
			}
		}
	}
</script>
<style>
	.placeholder {
		font-size: 26rpx;
		color: #999999;
	}
</style>
<style lang="scss" scoped>
	.page {
		height: 100vh;
		overflow: hidden;

		.list {
			padding: 24rpx;

			.item {
				background: #fff;
				height: 92rpx;
				box-sizing: border-box;
				padding: 28rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 20rpx;
				border-radius: 20rpx;

				.label {
					font-size: 28rpx;
					color: #333333;
				}

				.content {
					font-size: 26rpx;
					color: #999999;
					margin-top: 24rpx;
				}

				.getCode {
					background-color: #FEF4F5;
					padding-left: 30rpx;
					padding-right: 30rpx;
					padding-top: 5rpx;
					padding-bottom: 5rpx;
					border-radius: 50rpx;
					text-align: center;
					font-weight: 500;
					color: #F7635C;
					font-size: 24rpx;
				}
			}
		}

		.logout {
			margin: 176rpx auto;
			height: 92rpx;
			width: 702rpx;
			border-radius: 100rpx;
			background: #FF5866;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 28rpx;
			color: #FFFFFF;
		}
	}
</style>